package com.tn.properties;

import com.tn.exception.BaseException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ConfigurationProperties(prefix = "weigh")
@Slf4j
public class WeighLedIpProperties {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class WeighChannelIp{
        private String left;
        private String right;
    }

    private static Map<String,WeighChannelIp> weighLedIpMap;

    public void setWeighLedIpMap(Map<String, WeighChannelIp> weighLedIpMap) {
        WeighLedIpProperties.weighLedIpMap = weighLedIpMap;
        if(WeighLedIpProperties.weighLedIpMap != null){
            log.info("读取初始化地磅LED屏幕IP地址成功，LED屏幕IP信息为{}", WeighLedIpProperties.weighLedIpMap);
        }else{
            log.error("读取初始化地磅LED屏幕IP地址失败，请检查配置文件中是否配置地磅LED屏幕IP");
        }
    }


    public static WeighChannelIp getLedIp(String weighId){
        if(weighLedIpMap == null || weighLedIpMap.isEmpty()){
            return null;
        }
        WeighChannelIp weighChannelIp = weighLedIpMap.get(weighId);
        return weighChannelIp;
    }


}
